<?php
class General_Image {
    private $filename = '';
    private $mimetype = '';
    private $width = 0;
    private $height = 0;
    private $ratio = 0;
    private $internalImage = null;

    private $_mimeIndex = array(
            1 => 'image/gif',
            2 => 'image/jpeg',
            3 => 'image/png',
            4 => 'application/x-shockwave-flash',
            5 => 'image/psd',
            6 => 'image/bmp',
            7 => 'image/tiff',
            8 => 'image/tiff',
            9 => 'image/jpc',
            10 => 'image/jp2',
            11 => 'image/jpx',
            // 12 => 'jb2',
            // 13 => 'swc',
            14 => 'video/x-iff'
        );
    private $_extIndex = array(
            'image/gif' => '.gif',
            'image/jpeg' => '.jpg',
            'image/pjpeg' => '.jpg',
            'image/png' => '.png',
            'application/x-shockwave-flash' => 'swf',
            'image/psd' => '.psd',
            'image/bmp' => '.bmp',
            'image/tiff' => '.tif',
            'image/jpc' => '.jpc',
            'image/jp2' => '.jp2',
            'image/jpx' => '.jpx',
            'video/x-iff' => '.iff'
        );
	/**
	 * Enter description here...
	 *
	 * @param string|resource $imagefile
	 * @param string $mimetype required if $imagefile is a resource
	 * @return unknown
	 */
    public function __construct($imagefile, $mimetype = '')
    {
        if (is_string($imagefile)) {
            return $this->LoadFromFile($imagefile);
        } else {
            $this->width = imagesx($imagefile);
            $this->height = imagesy($imagefile);
            if (!empty($this->width) && !empty($this->height)) {
                $this->ratio = $this->width / $this->height;
                $this->internalImage = $imagefile;
                $this->filename = '';
                if (empty($mimetype)) {
                    $this->mimetype = '';
                } else {
                    $this->mimetype = $mimetype;
                }
                return true;
            } else {
                return false;
            }
        }
    }
    public function mimetype2extension($mimetype) {
    	return $this->_extIndex[$mimetype];
    }

    public function LoadFromFile($imagefile)
    {
        $imageinfo = getimagesize($imagefile);
        if ($imageinfo[0] > 0 && $imageinfo[1] > 0) {
            $this->width = $imageinfo[0];
            $this->height = $imageinfo[1];
            $this->ratio = $this->width / $this->height;
            if ($imageinfo['mime']) {
                $this->mimetype = $imageinfo['mime'];
            } elseif ($this->_mimeIndex[$imageinfo[2]]) {
                $this->mimetype = $this->_mimeIndex[$imageinfo[2]];
            } else {
                return false;
            }
            $this->filename = realpath($imagefile);
            if ($this->mimetype == 'image/jpeg') {
                $this->internalImage = imagecreatefromjpeg($this->filename);
            } elseif ($this->mimetype == 'image/png') {
                $this->internalImage = imagecreatefrompng($this->filename);
            } elseif ($this->mimetype == 'image/gif') {
                $this->internalImage = imagecreatefromgif($this->filename);
            } elseif ($this->mimetype == 'image/bmp') {
                $this->internalImage = imagecreatefromwbmp($this->filename);
            }
        } else {
            return false;
        }
    }

    public function Save($filename, $mimetype = '', $quality = 0, $autoExtension = true)
    {
        if (empty($mimetype)) {
            $mimetype = $this->mimetype;
        }
        if ($autoExtension && !preg_match(sprintf('#%s$#i', $this->_extIndex[$mimetype]), $filename)) {
            // append filename extension if filename does not end with it
            $filename .= $this->_extIndex[$mimetype];
        }
        if ($quality == 0) {
            $quality = 75;
        }
        if ($mimetype == 'image/jpeg') {
            $handle = fopen($filename, 'w+');
            ob_start();
            $result = imagejpeg($this->internalImage, "", $quality);
            fwrite($handle, ob_get_contents());
            fclose($handle);
            ob_end_clean();
            return $result;
        } elseif ($mimetype == 'image/png') {
            $handle = fopen($filename, 'w+');
            ob_start();
            $result = imagejpeg($this->internalImage);
            fwrite($handle, ob_get_contents());
            fclose($handle);
            ob_end_clean();
            return $result;
        } elseif ($mimetype == 'image/gif') {
            $handle = fopen($filename, 'w+');
            ob_start();
            $result = imagegif($this->internalImage);
            fwrite($handle, ob_get_contents());
            fclose($handle);
            ob_end_clean();
            return $result;
        } elseif ($mimetype == 'image/bmp') {
            $handle = fopen($filename, 'w+');
            ob_start();
            $result = imagewbmp($this->internalImage);
            fwrite($handle, ob_get_contents());
            fclose($handle);
            ob_end_clean();
            return $result;
        } else {
            return false;
        }
    }

    private function Resize($newWidth, $newHeight)
    {
        if (($this->mimetype == 'image/gif')) {
            $resizedImage = imagecreate($newWidth, $newHeight);
        } else {
            $resizedImage = imagecreatetruecolor($newWidth, $newHeight);
        }
        imagecopyresampled($resizedImage, $this->internalImage, 0, 0, 0, 0, $newWidth, $newHeight, $this->width, $this->height);
        // copy object
        $this->internalImage = $resizedImage;
        // initialize resized object
        return true;
    }

    private function ResizeBBox($maxWidth, $maxHeight)
    {
        if (($maxWidth == 0) && ($maxHeight == 0)) {
            return false;
        }
        if (($maxWidth == 0) && ($maxHeight != 0)) {
            // fixed height; width is determined by the aspect ratio
            $newWidth = round($maxHeight * $this->ratio);
            $newHeight = $maxHeight;
        } elseif (($maxWidth != 0) && ($maxHeight == 0)) {
            // fixed width; height is determined by the aspect ratio
            $newHeight = round($maxWidth / $this->ratio);
            $newWidth = $maxWidth;
        } else {
            $newWidth = round($maxHeight * $this->ratio);
            if ($newWidth <= $maxWidth) {
                $newHeight = $maxHeight;
            } else {
                $newHeight = round($maxWidth / $this->ratio);
                $newWidth = $maxWidth;
            }
        }
        return $this->Resize($newWidth, $newHeight);
    }

    private function Crop($left, $top, $width, $height)
    {
        if (($width > $this->width) || ($height > $this->height)) {
            trigger_error('Cannot crop image because it is too small', E_USER_WARNING);
            return false;
        }
        if (($this->mimetype == 'image/gif')) {
            $croppedImage = imagecreate($width, $height);
        } else {
            $croppedImage = imagecreatetruecolor($width, $height);
        }
        imagecopyresampled($croppedImage, $this->internalImage, 0, 0, $left, $top, $width, $height, $width, $height);
        // copy object
        $this->internalImage = $croppedImage;
        // initialize resized object
        return true;
    }

    private function Show($mimetype = '', $quality = 0)
    {
        if (!$mimetype) {
            $mimetype = $this->mimetype;
        }
        if ($quality == 0) {
            $quality = 75;
        }
        if (headers_sent()) {
            return false;
        }
        header('Content-type: ' . $mimetype);
        if ($mimetype == 'image/jpeg') {
            return imagejpeg($this->internalImage, '', $quality);
        } elseif ($mimetype == 'image/png') {
            return imagepng($this->internalImage);
        } elseif ($mimetype == 'image/gif') {
            return imagegif($this->internalImage);
        } elseif ($mimetype == 'image/bmp') {
            return imagewbmp($this->internalImage);
        } else {
            return false;
        }
    }
    
    public function ThumbnailCreate($width, $height, $cropParams = array())
    {
        if (empty($width) && empty($height)) {
            return false;
        }
        if (empty($cropParams)) {
            // resize only
            return $this->ResizeBBox($width, $height);
        }
        // determine ratio
        if (empty($cropParams['ThumbnailRatio'])) {
            if (empty($width) || empty($height)) {
                return false;
            } else {
                $thumbnailRatio = $width / $height;
            }
        } elseif (is_string($cropParams['ThumbnailRatio'])) {
            $thumbnailRatio = floatval(eval(sprintf('return %s;', $cropParams['ThumbnailRatio'])));
        } else {
            $thumbnailRatio = $cropParams['ThumbnailRatio'];
        }
        // prepare values
        if (!empty($width)) {
            $height = round($width / $thumbnailRatio);
        } else {
            $width = round($height * $thumbnailRatio);
        }
        $cropLeft = empty($cropParams['ThumbnailCropLeft']) ? 0 : (is_string($cropParams['ThumbnailCropLeft']) ? floatval(eval(sprintf('return %s;', $cropParams['ThumbnailCropLeft']))) : $cropParams['ThumbnailCropLeft']);
        $cropTop = empty($cropParams['ThumbnailCropTop']) ? 0 : (is_string($cropParams['ThumbnailCropTop']) ? floatval(eval(sprintf('return %s;', $cropParams['ThumbnailCropTop']))) : $cropParams['ThumbnailCropTop']);
        // crop it
        if ($this->ratio > $thumbnailRatio) {
            // width
            $result = $this->ResizeBBox(0, $height);
        } else {
            // height
            $result = $this->ResizeBBox($width, 0);
        }
        return $this->Crop(round($cropLeft * $img->width), round($cropTop * $img->height), $width, $height);
    }


   

}


